package com.chonor.mall.service.impl;

import com.chonor.mall.dto.BrandParam;
import com.chonor.mall.mapper.BrandMapper;
import com.chonor.mall.mapper.ProductMapper;
import com.chonor.mall.model.Brand;
import com.chonor.mall.model.BrandExample;
import com.chonor.mall.model.Product;
import com.chonor.mall.model.ProductExample;
import com.chonor.mall.service.BrandService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    
    @Autowired
    private BrandMapper brandMapper;
    
    @Autowired
    private ProductMapper productMapper;
    
    @Override
    public List<Brand> listAllBrand() {
        return brandMapper.selectByExample(new BrandExample());
    }

    @Override
    public int createBrand(BrandParam brandParam) {
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandParam, brand);
        brand.setShowStatus(1);
        brand.setProductCount(0);
        //如果创建时首字母为空，取名称的第一个为首字母
//        if (StringUtils.isEmpty(pmsBrand.getFirstLetter())) {
//            pmsBrand.setFirstLetter(pmsBrand.getName().substring(0, 1));
//        }
        return brandMapper.insertSelective(brand);
    }

    @Override
    public int updateBrand(Long id, BrandParam brandParam) {
        Brand pmsBrand = new Brand();
        BeanUtils.copyProperties(brandParam, pmsBrand);
        pmsBrand.setId(id);
        //如果创建时首字母为空，取名称的第一个为首字母
//        if (StringUtils.isEmpty(pmsBrand.getFirstLetter())) {
//            pmsBrand.setFirstLetter(pmsBrand.getName().substring(0, 1));
//        }
        //更新品牌时要更新商品中的品牌名称
        Product product = new Product();
        product.setBrandName(pmsBrand.getName());
        ProductExample example = new ProductExample();
        example.createCriteria().andBrandIdEqualTo(id);
        productMapper.updateByExampleSelective(product,example);
        return brandMapper.updateByPrimaryKeySelective(pmsBrand);
    }

    @Override
    public int deleteBrand(Long id) {
        return brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteBrand(List<Long> ids) {
        BrandExample pmsBrandExample = new BrandExample();
        pmsBrandExample.createCriteria().andIdIn(ids);
        return brandMapper.deleteByExample(pmsBrandExample);
    }

    @Override
    public List<Brand> listBrand(String keyword, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        BrandExample pmsBrandExample = new BrandExample();
        pmsBrandExample.setOrderByClause("sort desc");
        BrandExample.Criteria criteria = pmsBrandExample.createCriteria();
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andNameLike("%" + keyword + "%");
        }
        return brandMapper.selectByExample(pmsBrandExample);
    }

    @Override
    public Brand getBrand(Long id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    @Override
    public int showStatus(Long id, Integer status) {
        Brand brand = getBrand(id);
        brand.setShowStatus(status);
        int count = brandMapper.updateByPrimaryKey(brand);
        return count;
    }

    @Override
    public int updateShowStatus(List<Long> ids, Integer showStatus) {
        Brand pmsBrand = new Brand();
        pmsBrand.setShowStatus(showStatus);
        BrandExample pmsBrandExample = new BrandExample();
        pmsBrandExample.createCriteria().andIdIn(ids);
        return brandMapper.updateByExampleSelective(pmsBrand, pmsBrandExample);
    }

    @Override
    public int updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
        Brand pmsBrand = new Brand();
        pmsBrand.setFactoryStatus(factoryStatus);
        BrandExample pmsBrandExample = new BrandExample();
        pmsBrandExample.createCriteria().andIdIn(ids);
        return brandMapper.updateByExampleSelective(pmsBrand, pmsBrandExample);
    }
}
